package com.yxjme.redpacket;


import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.yxjme.redpackagelibrary.AddCountDownListener;
import com.yxjme.redpackagelibrary.CountDownUtil;
import com.yxjme.redpackagelibrary.RedPacket;
import com.yxjme.redpackagelibrary.RedPacketView;


public class MainActivity extends AppCompatActivity {


    private RedPacketView redRainView1;
    private TextView money, tv_red_package_value, tv_time;
    private int totalmoney = 0;


    int mCount = 10 * 60 ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        money = (TextView) findViewById(R.id.money);
        redRainView1 = findViewById(R.id.red_packets_view1);
        /*执行时间*/
        redRainView1.setTimeSeconds(60);

        /*支持点击消失*/
        redRainView1.setSupportClickDisappear(true);

        tv_red_package_value = findViewById(R.id.tv_red_package_value);
        tv_time = findViewById(R.id.tv_time);
        findViewById(R.id.start)
                .setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "adfasd", Toast.LENGTH_LONG).show();
                /*测试数据*/
                redRainView1.setTest(true,3000);

                startRedRain();

                countDownUtil =new CountDownUtil();
                countDownUtil.setTimeSeconds(60)
                        .addCountDownListener(new AddCountDownListener() {
                    @Override
                    public void onCountDowning(final CountDownUtil.TimeBean timeBean) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                StringBuffer stringBuffer = new StringBuffer();
                                stringBuffer.append(timeBean.h).append(":");
                                stringBuffer.append(timeBean.m).append(":");
                                stringBuffer.append(timeBean.sc);
                                tv_time.setText(stringBuffer.toString());
                            }
                        });
                    }

                    @Override
                    public void onCountComplete() {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Log.v("=====ggg=====","onCountComplete");
                            }
                        });
                    }

                    @Override
                    public void onError(String error) {

                    }

                            @Override
                            public void onReset() {

                            }
                        }).startCountDown();
            }
        });


        findViewById(R.id.stop).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                stopRedRain();
            }
        });
    }


    private CountDownUtil countDownUtil;


    /**
     * 开始下红包雨
     */
    private void startRedRain() {
        Log.v("======stadRain======", "startRedRain");
        redRainView1.startRain();
        redRainView1.setOnRedPacketClickListener(new RedPacketView.OnRedPacketClickListener() {
            @Override
            public void onRedPacketClickListener(RedPacket redPacket, MotionEvent motionEvent) {
                Log.v("======redPacket=====", "redPacket");
                totalmoney += redPacket.money;
                money.setText("中奖金额: " + totalmoney);

                int w = tv_red_package_value.getWidth();
                int h = tv_red_package_value.getHeight();
                tv_red_package_value.setTranslationX(motionEvent.getRawX() - w / 2);
                tv_red_package_value.setTranslationY(motionEvent.getRawY() - h / 2);
                tv_red_package_value.setText("+" + redPacket.money);
                tv_red_package_value.setVisibility(View.VISIBLE);
                tv_red_package_value.setAlpha(1);
                ObjectAnimator.ofFloat(tv_red_package_value, "scaleX", 1, 0).setDuration(500).start();
                ObjectAnimator.ofFloat(tv_red_package_value, "scaleY", 1, 0).setDuration(500).start();
            }
        });
    }

    /**
     * 停止下红包雨
     */
    private void stopRedRain() {
        if(countDownUtil!=null){
            countDownUtil.reset();
            countDownUtil = null ;
        }
        totalmoney = 0;//金额清零
        redRainView1.stopRainNow();
    }
}

